package xgame;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 序列化容器
 * 
 * @author AfritXia
 * @version $Rev: 15 $
 *
 */
public class XpropertyContainer {
	/**
	 * 内置字典
	 * 
	 */
	private Map<String, Object> _innerMap = null;

	/**
	 * 类默认构造器
	 * 
	 */
	public XpropertyContainer() {
	}

	/**
	 * 获取整型值
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public int getInt(String key) {
		Object value = this.getObject(key);
		
		if (value == null) {
			return 0;
		}

		return (Integer)value;
	}

	/**
	 * 设置整型值
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putInt(String key, int value) {
		this.putObject(key, value);
	}

	/**
	 * 获取整型数组
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public int[] getIntArray(String key) {
		return (int[])this.getObject(key);
	}

	/**
	 * 设置整型数组
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putIntArray(String key, int[] value) {
		this.putObject(key, value);
	}

	/**
	 * 获取字符
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public char getChar(String key) {
		Object value = this.getObject(key);

		if (value == null) {
			return (char)0;
		}

		return (Character)value;
	}

	/**
	 * 设置字符
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putChar(String key, char value) {
		this.putObject(key, value);
	}

	/**
	 * 获取字节数组
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public char[] getCharArray(String key) {
		return (char[])this.getObject(key);
	}

	/**
	 * 设置字节数组
	 * 
	 * @param key
	 * @param value
	 * 
	 */
	public void putCharArray(String key, char[] value) {
		this.putObject(key, value);
	}

	/**
	 * 获取字符串
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public String getString(String key) {
		return (String)this.getObject(key);
	}

	/**
	 * 设置字符串
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putString(String key, String value) {
		this.putObject(key, value);
	}

	/**
	 * 获取字符串数组
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public String[] getStringArray(String key) {
		return (String[])this.getObject(key);
	}

	/**
	 * 设置字符串数组
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putStringArray(String key, String[] value) {
		this.putObject(key, value);
	}

	/**
	 * 获取日期 
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public Date getDate(String key) {
		return (Date)this.getObject(key);
	}

	/**
	 * 设置日期
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putDate(String key, Date value) {
		this.putObject(key, value);
	}

	/**
	 * 获取日期 数组
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public Date[] getDateArray(String key) {
		return (Date[])this.getObject(key);
	}

	/**
	 * 设置日期数组
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putDateArray(String key, Date[] value) {
		this.putObject(key, value);
	}

	/**
	 * 获取长整型
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public long getLong(String key) {
		Object value = this.getObject(key);

		if (value == null) {
			return 0L;
		}

		return (Long)value;
	}

	/**
	 * 设置长整型
	 * 
	 * @param key
	 * @param value
	 * 
	 */
	public void putLong(String key, long value) {
		this.putObject(key, value);
	}

	/**
	 * 获取长整型
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public long[] getLongArray(String key) {
		return (long[])this.getObject(key);
	}

	/**
	 * 设置长整型数组
	 * 
	 * @param key
	 * @param value
	 * 
	 */
	public void putLongArray(String key, long[] value) {
		this.putObject(key, value);
	}

	/**
	 * 获取短整型
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public short getShort(String key) {
		Object value = this.getObject(key);

		if (value == null) {
			return 0;
		}

		return (Short)value;
	}

	/**
	 * 设置短整型
	 * 
	 * @param key
	 * @param value
	 * 
	 */
	public void putShort(String key, short value) {
		this.putObject(key, value);
	}

	/**
	 * 获取短整型数组
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public short[] getShortArray(String key) {
		return (short[])this.getObject(key);
	}

	/**
	 * 设置短整型数组
	 * 
	 * @param key
	 * @param value
	 * 
	 */
	public void putShortArray(String key, short[] value) {
		this.putObject(key, value);
	}

	/**
	 * 获取布尔值
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public boolean getBoolean(String key) {
		Object value = this.getObject(key);

		if (value == null) {
			return false;
		}

		return (Boolean)value;
	}

	/**
	 * 设置布尔值
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putBoolean(String key, boolean value) {
		this.putObject(key, value);
	}

	/**
	 * 获取布尔值数组
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public boolean[] getBooleanArray(String key) {
		return (boolean[])this.getObject(key);
	}

	/**
	 * 设置布尔值数组
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putBooleanArray(String key, boolean[] value) {
		this.putObject(key, value);
	}

	/**
	 * 设置字节
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public byte getByte(String key) {
		Object value = this.getObject(key);

		if (value == null) {
			return 0;
		}

		return (Byte)value;
	}

	/**
	 * 获取字节
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putByte(String key, byte value) {
		this.putObject(key, value);
	}

	/**
	 * 设置字节数组
	 * 
	 * @param key
	 * @return
	 * 
	 */
	public byte[] getByteArray(String key) {
		return (byte[])this.getObject(key);
	}

	/**
	 * 获取字节数组
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putByteArray(String key, byte[] value) {
		this.putObject(key, value);
	}

	/**
	 * 获取属性容器
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public XpropertyContainer getPropertyContainer(String key) {
		return (XpropertyContainer)this.getObject(key);
	}

	/**
	 * 设置属性容器
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putPropertyContainer(String key, XpropertyContainer value) {
		this.putObject(key, value);
	}

	/**
	 * 获取属性容器数组
	 * 
	 * @param key
	 * @return 
	 * 
	 */
	public XpropertyContainer[] getProperContainerArray(String key) {
		return (XpropertyContainer[])this.getObject(key);
	}

	/**
	 * 设置属性容器数组
	 * 
	 * @param key
	 * @param value 
	 * 
	 */
	public void putPropertyContainerArray(String key, XpropertyContainer[] value) {
		this.putObject(key, value);
	}

	/**
	 * 获取键值
	 * 
	 * @param key 
	 * @return 
	 * 
	 */
	private Object getObject(String key) {
		if (key == null) {
			throw new IllegalArgumentException("key");
		}

		return this.getInnerMap().get(key);
	}

	/**
	 * 设置键值
	 * 
	 * @param key 
	 * @param value 
	 * 
	 */
	private void putObject(String key, Object value) {
		if (key == null) {
			throw new IllegalArgumentException("key");
		}

		this.getInnerMap().put(key, value);
	}

	/**
	 * 获取内置字典
	 * 
	 * @return 
	 * 
	 */
	private Map<String, Object> getInnerMap() {
		if (this._innerMap == null) {
			this._innerMap = new HashMap<String, Object>();
		}

		return this._innerMap;
	}

	/**
	 * 友元类
	 * 
	 * @author AfritXia
	 *
	 */
	public static class Friend {
		/**
		 * 获取内置字典
		 * 
		 * @return 
		 * 
		 */
		protected Map<String, Object> getInnerMap(XpropertyContainer container) {
			if (container == null) {
				return null;
			}

			return container.getInnerMap();
		}
	}
}
